home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter7 / sort.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  273 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "sort.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "TreeView_SortChildren()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. VOID AddItemsToTree( HWND hTree );
  107. int CALLBACK SortCompare( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort );
  108.  
  109. #define IMAGESIZE 16
  110.  
  111. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  112.  
  113. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  114. {
  115. static HWND hTree = NULL;
  116.  
  117.    switch( uMsg )
  118.    {
  119.       case WM_CREATE  :
  120.               {
  121.                  HIMAGELIST hImage;
  122.  
  123.                  InitCommonControls();
  124.  
  125.                  hImage = ImageList_Create( IMAGESIZE, IMAGESIZE, 
  126.                                             ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  127.                  
  128.                  hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
  129.                                          WS_CHILD | WS_BORDER | WS_VISIBLE | 
  130.                                          WS_VSCROLL | TVS_HASLINES | 
  131.                                          TVS_HASBUTTONS | TVS_LINESATROOT |
  132.                                          TVS_DISABLEDRAGDROP,
  133.                                          10, 100, 100, 100,
  134.                                          hWnd,
  135.                                          (HMENU)1,
  136.                                          hInst,
  137.                                          NULL);
  138.  
  139.    
  140.                  if ( hTree )
  141.                  {
  142.                     int i;
  143.  
  144.                     // Associate the image list with the tree view.
  145.                     //.............................................
  146.                     TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
  147.  
  148.                     // Add the images to the tree view image list.
  149.                     //............................................
  150.                     for ( i=0; i<4; i++ )
  151.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  152.  
  153.                     AddItemsToTree( hTree );
  154.                  }
  155.               }
  156.               break;
  157.  
  158.  
  159.       case WM_SIZE :
  160.               if ( hTree )
  161.                  MoveWindow( hTree,0,0, LOWORD(lParam), HIWORD(lParam), FALSE );
  162.               break;
  163.  
  164.  
  165.       case WM_COMMAND :
  166.               switch( LOWORD( wParam ) )
  167.               {
  168.                  case IDM_SORTALPHA :
  169.                         TreeView_SortChildren( hTree, NULL, 0 );
  170.                         break;
  171.  
  172.                  case IDM_SORTNUMERIC :
  173.                         {
  174.                            TV_SORTCB tvs;
  175.  
  176.                            tvs.hParent     = NULL;
  177.                            tvs.lpfnCompare = SortCompare;
  178.                            tvs.lParam      = 0;
  179.  
  180.                            TreeView_SortChildrenCB( hTree, &tvs, 0 );
  181.                         }
  182.                         break;
  183.                   
  184.                  case IDM_ABOUT :
  185.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  186.                         break;
  187.  
  188.                  case IDM_EXIT :
  189.                         DestroyWindow( hWnd );
  190.                         break;
  191.               }
  192.               break;
  193.       
  194.       case WM_DESTROY :
  195.  
  196.               if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
  197.                  ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
  198.  
  199.               PostQuitMessage(0);
  200.               break;
  201.  
  202.       default :
  203.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  204.    }
  205.  
  206.    return( 0L );
  207. }
  208.  
  209.  
  210. int CALLBACK SortCompare( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort )
  211. {
  212.    if ( lParam1 < lParam2 )
  213.       return( -1 );
  214.  
  215.    if ( lParam1 > lParam2 )
  216.       return( 1 );
  217.  
  218.    return( 0 );
  219. }
  220.  
  221.  
  222. VOID AddItemsToTree( HWND hTree )
  223. {
  224.    char            szBuf[40];
  225.    int             i;
  226.    TV_INSERTSTRUCT tv;
  227.  
  228.    tv.hInsertAfter = TVI_LAST;
  229.    tv.item.mask    = TVIF_TEXT | TVIF_IMAGE | 
  230.                      TVIF_SELECTEDIMAGE | TVIF_PARAM;
  231.  
  232.    // Add the items to the tree view.
  233.    //................................
  234.    for ( i=0; i<4; i++ )
  235.    {
  236.       wsprintf( szBuf, "%s [%d]", lpszData[i], i+1 );
  237.  
  238.       tv.hParent      = TVI_ROOT;
  239.       tv.item.pszText = szBuf;
  240.       tv.item.iImage  = i;
  241.       tv.item.iSelectedImage = i;
  242.       tv.item.lParam         = i+1;
  243.  
  244.       TreeView_InsertItem( hTree, &tv );
  245.    }
  246. }
  247.  
  248.  
  249. LRESULT CALLBACK About( HWND hDlg,           
  250.                         UINT message,        
  251.                         WPARAM wParam,       
  252.                         LPARAM lParam)
  253. {
  254.    switch (message) 
  255.    {
  256.        case WM_INITDIALOG: 
  257.                return (TRUE);
  258.  
  259.        case WM_COMMAND:                              
  260.                if (   LOWORD(wParam) == IDOK         
  261.                    || LOWORD(wParam) == IDCANCEL)    
  262.                {
  263.                        EndDialog(hDlg, TRUE);        
  264.                        return (TRUE);
  265.                }
  266.                break;
  267.    }
  268.  
  269.    return (FALSE); 
  270. }
  271.  
  272.  
  273.